import {createStore} from "vuex";
import createPersistedState from 'vuex-persistedstate'

const store = createStore({
    state: {
        isLogin: false,
        userinfo: {
            uid: "",
            id: ""
        },
        msgState: {
            isShowing: false,
            msgValue: "",
            msgType: 'error'
        },
        bxfbData:{
            // 这个数据用于靶向发布页面
            longitude: "",
            latitude: "",
            temperature: "",
            humidity: "",
            windSpeed: "",
            visibility: "",
            pressure: "",
            windDirection: "",
            degree: "",
        }
    },
    getters: {
        isLogin: state => state.isLogin,
        userId: state => state.userinfo.id,
        bxfbData: state => state.bxfbData
    },
    plugins: [
        createPersistedState(),
    ],
    mutations: {
        Loginin(state, userinfo) {
            state.isLogin = true
            state.userinfo.uid = userinfo.uid
            state.userinfo.id = userinfo.id
        },
        Loginout(state) {
            state.isLogin = false
            state.userinfo.uid = ''
            console.log('logout')
        },
        alertMsg(state, params) {
            state.msgState.msgValue = params.msg ? params.msg : 'msg';
            state.msgState.msgType = params.type ? params.type : 'error';
            state.msgState.isShowMsg = true;
            setTimeout(() => {
                state.msgState.isShowMsg = false;
            }, params.time ? params.time : 3000);
        },
        updateBxfbData(state, newData) {
            state.bxfbData = newData;
        },
    }
});

export default store;
